package filters;

import interfaces.IMap;

public class LocalBinarizator implements IFilter
{
	int Histogram[];
	int LowLevel;
	//===========================================================================
	public LocalBinarizator(int lowLevel)
	{
		LowLevel = lowLevel;
		Histogram = new int[256];
	}
	//===========================================================================
	@Override
	public int ApplyFilter(int Px, int Py, IMap map) 
	{
		int cur = (int)(map.Get(Px,Py));
		if(cur<LowLevel)
			return 0;
		
		for(int i=0;i<256;i++)
			Histogram[i]=0;
		
		for (int y = -3; y < 4; y++)
            for (int x = -3; x < 4; x++)
            {            	
            	int C = ((int)map.Get(Px + x,Py + y) & 255);
            	Histogram[C]++;            	
            }
		int sum = 0;
		for(int i=0;i<256;i++)
			sum += (i+1)*Histogram[i];	
		
		return (sum/49)<cur?255:0;
	}
	//===========================================================================	
}
